package lo

import "fmt"

func Add(s []int, element int, index int) ([]int, error) {
	if index < 0 || index > len(s) {
		return nil, fmt.Errorf("index out of range")
	}
	s1 := make([]int, 0, len(s)+1)
	s1 = append(s1, s[:index]...)
	s1 = append(s1, element)
	s1 = append(s1, s[index:]...)
	return s1, nil
}

func Remove(s []int, index int) ([]int, error) {
	if index < 0 || index > len(s) {
		return nil, fmt.Errorf("index out of range")
	}
	s1 := make([]int, 0, len(s)-1)
	s1 = append(s1, s[:index]...)
	s1 = append(s1, s[index+1:]...)
	return s1, nil
}

func Search(s []int, element int) (int, error) {
	for i, value := range s {
		if value == element {
			return i, nil
		}
	}
	return -1, fmt.Errorf("element not found")
}